/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
/*
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
 */
import { createRouter, createWebHashHistory } from "vue-router";
import { useAccount } from "../store/user";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      component: () => import("../views/main.vue"),
      children: [
        {
          path: "",
          component: () => import("../views/home.vue"),
        },
        {
          path: "/job",
          component: () => import("../views/job.vue"),
        },
        {
          path: "/mine",
          component: () => import("../views/mine.vue"),
        },
      ],
    },
    {
      path: "/detail/:id",
      component: () => import("../views/detail.vue"),
    },
    {
      path: "/edit",
      component: () => import("../views/edit.vue"),
    },
    {
      path: "/test",
      component: () => import("../views/test/index.vue"),
    },
    {
      path: "/test/rem",
      component: () => import("../views/test-rem.vue"),
    },
    {
      path: "/test/pinia",
      component: () => import("../views/test-pinia/index.vue"),
    },
    {
      path: "/login",
      component: () => import("../views/login.vue"),
    },
  ],
});
const blackList = ["/mine", "/edit"]; //路由黑名单
router.beforeEach((to, _, next) => {
  if (!blackList.includes(to.path)) {
    next(); //不在黑名单内的路由，直接放行
  } else {
    const user = useAccount();
    if (user.userInfo) {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
